package com.sinosoft.configuration;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpMethod;

/**
 * @author loujiang
 * @date 2020/12/31 3:57 下午
 * @NOTE: Gateway代码配置方式
 */
@Configuration
public class GatewayConfiguration {



    @Autowired
    private TimerFilter timerFilter;


     @Autowired
     private AuthFilter authFilter;


    @Bean
    public GlobalFilter printRquestFilter() {
        return new PrintRequestFilter();
    }


    @Bean
    @Order
    public RouteLocator customerRouteLocator(RouteLocatorBuilder builder) {

        // System.out.println("123456");
        // Predicates  匹配是否包含java ,和是否是Get 请求  ，
        // filter 截取1个路径 向RequestHeader 添加 username=sa,
        return   builder.routes().route(r->r.path("/java/**")
                .and()
                .method(HttpMethod.GET).
                        filters(f-> f.stripPrefix(1).
                                addRequestHeader("username","sam")
                                .addResponseHeader("test","jack").filter(timerFilter).filter(authFilter)
                        )
                .uri("lb://user-center")).build();

    }


}
